/**
 * 
 */
package com.depot.auth.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.base.KeyGenerator;
import com.depot.domains.AppMenu;
import com.soar.context.Environment;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SortTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.util.StringUtils;

public class AppMenuDao
{
	/**
	 * 应用菜单列表
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 * */
	public static List<HashMap<String, String>> select(Environment env, AppMenu domain) throws SQLException
	{
		DbParameter param = new DbParameter("APP_MENU T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.PID,");
		sb.append("T1.MU_NAME,");
		sb.append("T1.MU_ORDER,");
		sb.append("T1.MU_ICON,");
		sb.append("T1.MU_URL,");
		sb.append("T1.MU_DISPLAY,");
		sb.append("T1.MU_PLATFORM,");
		sb.append("T1.MU_LEVEL,");
		sb.append("T1.OPEN_MODE,");
		sb.append("T1.MU_MODE,");
		sb.append("T1.MU_POSTION,");
		sb.append("T1.REQUEST_METHOD,");
		sb.append("T1.MODE_CODE,");
		sb.append("T1.CREATE_USER_ID,");
		sb.append("T1.CREATE_USER,");
		sb.append("T1.CREATE_DATE,");
		sb.append("T1.UPDATE_USER_ID,");
		sb.append("T1.UPDATE_USER,");
		sb.append("T1.UPDATE_DATE");
		param.setSelectFields(sb.toString());
		if (StringUtils.isNoEmpty(domain.getSearchMuName()))
		{
			param.setWhereParams("T1.MU_NAME", domain.getSearchMuName(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchMuDisplay()))
		{
			param.setWhereParams("T1.MU_DISPLAY", domain.getSearchMuDisplay(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchMuPlatform()))
		{
			param.setWhereParams("T1.MU_PLATFORM", domain.getSearchMuPlatform(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchMuLevel()))
		{
			param.setWhereParams("T1.MU_LEVEL", domain.getSearchMuLevel(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchOpenMode()))
		{
			param.setWhereParams("T1.OPEN_MODE", domain.getSearchOpenMode(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchMuMode()))
		{
			param.setWhereParams("T1.MU_MODE", domain.getSearchMuMode(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchMuPostion()))
		{
			param.setWhereParams("T1.MU_POSTION", domain.getSearchMuPostion(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchRequestMethod()))
		{
			param.setWhereParams("T1.REQUEST_METHOD", domain.getSearchRequestMethod(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchModeCode()))
		{
			param.setWhereParams("T1.MODE_CODE", domain.getSearchModeCode(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		param.setOrderParams("MU_ORDER", SortTypes.ASC);
		return DbTemplates.select(env.getConnection(), param);
	}

	/**
	 * 单行查询
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static HashMap<String, String> doShow(Environment env, AppMenu domain) throws SQLException
	{
		DbParameter param = new DbParameter("APP_MENU T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.PID,");
		sb.append("T1.MU_NAME,");
		sb.append("T1.MU_ORDER,");
		sb.append("T1.MU_ICON,");
		sb.append("T1.MU_URL,");
		sb.append("T1.MU_DISPLAY,");
		sb.append("T1.MU_PLATFORM,");
		sb.append("T1.MU_LEVEL,");
		sb.append("T1.OPEN_MODE,");
		sb.append("T1.MU_MODE,");
		sb.append("T1.MU_POSTION,");
		sb.append("T1.REQUEST_METHOD,");
		sb.append("T1.MODE_CODE,");
		sb.append("T1.CREATE_USER_ID,");
		sb.append("T1.CREATE_USER,");
		sb.append("T1.CREATE_DATE,");
		sb.append("T1.UPDATE_USER_ID,");
		sb.append("T1.UPDATE_USER,");
		sb.append("T1.UPDATE_DATE");
		param.setSelectFields(sb.toString());
		param.setWhereParams("T1.ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		return DbTemplates.select(env.getConnection(), param).get(0);
	}

	/**
	 * 应用菜单修改
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public static String doSave(Environment env, AppMenu domain) throws SQLException
	{
		DbParameter param = null;
		if (StringUtils.isEmpty(domain.getId()))
		{// ID为空 新增
			param = new DbParameter("app_menu T1");
			param.setParams("ID", KeyGenerator.getInstance().getKeyId(env.getConnection()), SqlTypes.VARCHAR);
			if (StringUtils.isNoEmpty(domain.getPid()))
			{
				param.setParams("PID", domain.getPid(), SqlTypes.VARCHAR);
			}
			param.setParams("MU_NAME", domain.getMuName(), SqlTypes.VARCHAR);
			param.setParams("MU_ORDER", domain.getMuOrder(), SqlTypes.CUSTOM);
			param.setParams("MODE_CODE", domain.getModeCode(), SqlTypes.VARCHAR);
			if (StringUtils.isNoEmpty(domain.getMuLevel()))
			{
				param.setParams("MU_LEVEL", Integer.parseInt(domain.getMuLevel()) + 1, SqlTypes.VARCHAR);
			}
			if (StringUtils.isNoEmpty(domain.getMuDisplay()))
			{
				param.setParams("MU_DISPLAY", domain.getMuDisplay(), SqlTypes.CHAR);
			}
			else
			{
				param.setParams("MU_DISPLAY", "1", SqlTypes.CHAR);
			}
			if (StringUtils.isNoEmpty(domain.getRequestMethod()))
			{
				param.setParams("REQUEST_METHOD", domain.getRequestMethod(), SqlTypes.CHAR);
			}
			param.setParams("MU_POSTION", domain.getMuPostion(), SqlTypes.CHAR);
			param.setParams("MU_ICON", domain.getMuIcon(), SqlTypes.VARCHAR);
			param.setParams("MU_URL", domain.getMuUrl(), SqlTypes.VARCHAR);
			param.setParams("OPEN_MODE", domain.getOpenMode(), SqlTypes.CHAR);
			param.setParams("MU_MODE", domain.getMuMode(), SqlTypes.CHAR);
			param.setParams("MU_PLATFORM", domain.getMuPlatform(), SqlTypes.CHAR);
			param.setParams("CREATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("CREATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("CREATE_DATE", "sysdate", SqlTypes.CUSTOM);
			param.setParams("UPDATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("UPDATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("UPDATE_DATE", "sysdate", SqlTypes.CUSTOM);
			if (DbTemplates.insert(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
		else
		{// ID不为空 修改
			param = new DbParameter("app_menu T1");
			param.setParams("MU_NAME", domain.getMuName(), SqlTypes.VARCHAR);
			param.setParams("MU_ORDER", domain.getMuOrder(), SqlTypes.CUSTOM);
			param.setParams("MU_ICON", domain.getMuIcon(), SqlTypes.VARCHAR);
			param.setParams("MU_URL", domain.getMuUrl(), SqlTypes.VARCHAR);
			param.setParams("MU_DISPLAY", domain.getMuDisplay(), SqlTypes.CHAR);
			param.setParams("MU_PLATFORM", domain.getMuPlatform(), SqlTypes.CHAR);
			// param.setParams("MU_LEVEL", domain.getMuLevel(), SqlTypes.CHAR);
			param.setParams("OPEN_MODE", domain.getOpenMode(), SqlTypes.CHAR);
			param.setParams("MU_MODE", domain.getMuMode(), SqlTypes.CHAR);
			param.setParams("MU_POSTION", domain.getMuPostion(), SqlTypes.CHAR);
			param.setParams("REQUEST_METHOD", domain.getRequestMethod(), SqlTypes.CHAR);
			param.setParams("MODE_CODE", domain.getModeCode(), SqlTypes.VARCHAR);
			param.setParams("UPDATE_USER_ID", env.getLoginUser().getUserId(), SqlTypes.CHAR);
			param.setParams("UPDATE_USER", env.getLoginUser().getUserName(), SqlTypes.VARCHAR);
			param.setParams("UPDATE_DATE", "sysdate", SqlTypes.CUSTOM);
			param.setWhereParams("ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			if (DbTemplates.update(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
	}

	/**
	 * 应用菜单删除
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public static String doDelete(Environment env, AppMenu domain) throws SQLException
	{
		if (domain.getCids() != null)
		{
			for (int i = 0; i < domain.getCids().length; i++)
			{
				if (!"1".equals(deleteByPid(env.getConnection(), domain.getCids()[i])))
				{
					env.getConnection().rollback();
					return "0";
				}
			}
		}
		else
		{
			return deleteByPid(env.getConnection(), domain.getId());
		}
		return "1";
	}

	public static String deleteByPid(Connection conn, String pid) throws SQLException
	{
		DbParameter param = new DbParameter("app_menu T1");
		param.setSelectFields("T1.ID,T1.PID");
		param.setWhereParams("T1.PID", pid, SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		List<HashMap<String, String>> list = DbTemplates.select(conn, param);
		for (HashMap<String, String> hashMap : list)
		{
			if (!"1".equals(deleteByPid(conn, hashMap.get("ID"))))
			{
				conn.rollback();
				return "0";
			}
		}
		param = new DbParameter("APP_ROLE_AUTH T1");
		param.setWhereParams("T1.MENU_ID", pid, SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		DbTemplates.delete(conn, param);
		param = new DbParameter("app_menu T1");
		param.setWhereParams("T1.ID", pid, SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		DbTemplates.delete(conn, param);
		return "1";
	}

	public static String updateOrder(Environment env, AppMenu domain) throws SQLException
	{
		for (int i = 0; i < domain.getIds().length; i++)
		{

			DbParameter param = new DbParameter("app_menu");
			if (StringUtils.isNoEmpty(domain.getOrders()[i]))
			{
				param.setParams("MU_ORDER", domain.getOrders()[i], SqlTypes.CUSTOM);
			}
			else
			{
				param.setParams("MU_ORDER", 0, SqlTypes.CUSTOM);
			}
			param.setWhereParams("ID", domain.getIds()[i], SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
			if (!DbTemplates.update(env.getConnection(), param))
			{
				env.getConnection().rollback();
				return "0";
			}

		}
		return "1";
	}

	/**
	 * 更新状态
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static String doUpdateStatus(Environment env, AppMenu domain) throws SQLException
	{
		DbParameter param = null;
		param = new DbParameter("APP_MENU T1");
		param.setParams("MU_DISPLAY", domain.getMuDisplay(), SqlTypes.CHAR);
		if (domain.getCids() == null)
		{
			param.setWhereParams("T1.ID", domain.getId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		}
		else
		{
			param.setWhereParams("T1.ID", param.getInSQL(domain.getCids()), SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.IN);
		}

		if (DbTemplates.update(env.getConnection(), param))
		{
			return "1";
		}
		else
		{
			return "0";
		}
	}

	public static String getuserRolecode(Environment env, String userid) throws SQLException
	{
		DbParameter param = new DbParameter("USER_ROLE t1,APP_ROLE t2");
		param.setSelectFields("t2.ROLE_CODE");
		param.setWhereParams("t1.USER_ID", userid, SqlTypes.CHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("T2. ID ", "T1.ROLE_ID", SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.YES);
		List<HashMap<String, String>> li = DbTemplates.select(env.getConnection(), param);
		if (li.size() > 0)
		{
			return li.get(0).get("ROLE_CODE");
		}
		else
		{
			return null;
		}

	}
}